//
//  DesignOfStoreViewController.m
//  Beauty
//
//  Created by Snow on 16/9/19.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "DesignOfStoreViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfBrandStoreGood.h"
#import "BrandDetailGoodTableViewCell.h"
#import "NewProductViewController.h"
@interface DesignOfStoreViewController ()<UITableViewDelegate, UITableViewDataSource, BrandDetailDelegate>

@property (nonatomic, strong) NSMutableArray *arrayOfGood;
@property (nonatomic, strong) NSDictionary *dicOfStore;
@property (nonatomic, strong) UIImageView *backImageViewForHearderView;
@property (nonatomic, strong) UIView *backViewForTableViewHeader;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation DesignOfStoreViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createView];
}
- (void)handleData
{
    NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=store&op=store_detail&store_id=%@", _store_id];
    _arrayOfGood = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        _dicOfStore = [NSDictionary dictionaryWithDictionary:responseObject[@"datas"][@"store_info"]];
        NSArray *arr = responseObject[@"datas"][@"rec_goods_list"];
        UIView *viewOfnavigation = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
        self.navigationItem.titleView = viewOfnavigation;
        UILabel *label = [[UILabel alloc]initWithFrame:viewOfnavigation.bounds];
        label.textAlignment = 1;
        label.textColor = [UIColor whiteColor];
        label.text = _dicOfStore[@"store_name"];
        [viewOfnavigation addSubview:label];
        for (NSDictionary *dic in arr) {
            ModelOfBrandStoreGood *model = [[ModelOfBrandStoreGood alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfGood addObject:model];
        }
        _tableView.tableHeaderView = [self createTableViewHeaderView];
        [_tableView reloadData];
    } filed:^(NSError *error) {
    
    }];
}
- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 49) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[BrandDetailGoodTableViewCell class] forCellReuseIdentifier:@"BrandDetailGoodTableViewCell"];
    [self.view addSubview:_tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BrandDetailGoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BrandDetailGoodTableViewCell"];
    cell.delegate = self;
    cell.arrOfModel = _arrayOfGood;
    return cell;
}
- (UIView *)createTableViewHeaderView
{
    self.backViewForTableViewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    self.backImageViewForHearderView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.backViewForTableViewHeader.bounds.size.height / 2)];
    [self.backViewForTableViewHeader addSubview:self.backImageViewForHearderView];
    [self.backImageViewForHearderView sd_setImageWithURL:[NSURL URLWithString:_dicOfStore[@"app_banner"]]];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, self.backViewForTableViewHeader.bounds.size.height / 2, 80, 80)];
    image.layer.masksToBounds = YES;
    image.layer.cornerRadius = 40;
    [image sd_setImageWithURL:[NSURL URLWithString:_dicOfStore[@"store_avatar"]]];
    [self.backViewForTableViewHeader addSubview:image];
    
    UILabel *labelOfName = [[UILabel alloc]initWithFrame:CGRectMake(0, self.backViewForTableViewHeader.bounds.size.height / 2 + 90, SCREEN_WIDTH, 30)];
    labelOfName.textAlignment = 1;
    labelOfName.text = _dicOfStore[@"store_name"];
    [self.backViewForTableViewHeader addSubview:labelOfName];
    labelOfName.font = [UIFont systemFontOfSize:20];
    UILabel *labelOfAdress = [[UILabel alloc]initWithFrame:CGRectMake(0, self.backViewForTableViewHeader.bounds.size.height / 2 + 130, SCREEN_WIDTH, 30)];
    labelOfAdress.textAlignment = 1;
    labelOfAdress.text = _dicOfStore[@"area_info"];
    [self.backViewForTableViewHeader addSubview:labelOfAdress];
    labelOfAdress.textColor = [UIColor lightGrayColor];
    UIButton *buttonOfDetail = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonOfDetail.frame = CGRectMake(0, self.backViewForTableViewHeader.bounds.size.height / 2 + 170, SCREEN_WIDTH, 30);
    [buttonOfDetail setTitle:_dicOfStore[@"store_description"] forState:UIControlStateNormal];
    buttonOfDetail.tintColor = [UIColor lightGrayColor];
    [self.backViewForTableViewHeader addSubview:buttonOfDetail];
    [buttonOfDetail addTarget:self action:@selector(handleDetail:) forControlEvents:UIControlEventTouchUpInside];
    return self.backViewForTableViewHeader;
}
- (void)handleDetail:(UIButton *)button
{
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    UILabel *label = [[UILabel alloc]initWithFrame:view.bounds];
    [view addSubview:label];
    label.text = @"心水推荐";
    label.textAlignment = 1;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200 * (_arrayOfGood.count / 2 + _arrayOfGood.count % 2) + 10;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset_y = scrollView.contentOffset.y;
    if (offset_y < 0) {
        CGFloat add_height = - (offset_y);
        CGFloat scale = (200 + add_height) / 200.0f;
        self.backImageViewForHearderView.frame = CGRectMake(-(SCREEN_WIDTH * scale - SCREEN_WIDTH) / 2.0f, -add_height, SCREEN_WIDTH * scale, 200 + add_height);
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

//页面跳转- 展示货物详情页
- (void)brandDetailWithGood_id:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc]init];
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}


@end
